//
//  DetailViewController.m
//  豆瓣masonry
//
//  Created by dllo on 15/12/14.
//  Copyright © 2015年 SMH. All rights reserved.
//

#import "DetailViewController.h"
#import "Masonry.h"


@interface DetailViewController ()

@property (nonatomic, retain)NSMutableArray *arr;

@property (nonatomic, assign)BOOL saved;

@property (nonatomic, assign)BOOL exist;

@end

@implementation DetailViewController




- (void)localFile {

    NSFileManager *manage = [NSFileManager defaultManager];
    
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"model.xml"];
    
    BOOL exist = [manage fileExistsAtPath:filePath];
    
    if (exist == YES) {
        

        
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:[NSData dataWithContentsOfFile:filePath]];
        
        NSArray *arrModel = [unarchiver decodeObjectForKey:@"arr"];
        
        [unarchiver finishDecoding];
        
        
        
        for (Model *model  in arrModel) {
            if ([model.title isEqualToString:self.model.title]) {

                self.saved = YES;
            }
        }
        
    } else {
        
        return;
    }
    
    
}




- (void)createView {

    UILabel *labelTitle = [[UILabel alloc] init];
    labelTitle.font = [UIFont systemFontOfSize:18];
    labelTitle.text = self.model.title;
    [self.view addSubview:labelTitle];
    

    [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(60);
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(50);
        
    }];
    
    UIImageView *imageViewPhoto = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1f21352e2418d18f781920d1c01667f1.jpg"]];
    //    imageViewPhoto.frame = CGRectMake(30, 140, 100, 150);
    imageViewPhoto.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:imageViewPhoto];
    

    UIImageView *imageViewTime = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_date_blue@2x"]];
    //    imageViewTime.frame = CGRectMake(140, 143, 20, 20);
    [self.view addSubview:imageViewTime];

    
    
    

    UILabel *labelTime = [[UILabel alloc] init];
    NSRange rang = {5, 11};
    labelTime.text = [NSString stringWithFormat:@"%@ -- %@", [self.model.begin_time substringWithRange:rang], [self.model.end_time substringWithRange:rang]];
    labelTime.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:labelTime];

    

    UIImageView *imageViewOwner_name = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_sponsor_blue@2x"]];

    [self.view addSubview:imageViewOwner_name];

    

    UILabel *labelOwner_name = [[UILabel alloc] init];
    labelOwner_name.font = [UIFont systemFontOfSize:14];
    labelOwner_name.text = [self.model.owner objectForKey:@"name"];
    [self.view addSubview:labelOwner_name];

    

    UIImageView *imageViewType = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_catalog_blue@2x"]];
    //    imageViewType.frame = CGRectMake(140, 196, 20, 20);
    [self.view addSubview:imageViewType];

//initWithFrame:CGRectMake(165, 192, 180, 25)];
    UILabel *labelType = [[UILabel alloc] init];
    labelType.font = [UIFont systemFontOfSize:14];
    labelType.text = [NSString stringWithFormat:@"类型:%@", self.model.category_name];
    [self.view addSubview:labelType];

    
 
    UIImageView *imageAddress = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_spot_blue@2x"]];
    imageAddress.frame = CGRectMake(140, 224, 20, 20);
    
    [self.view addSubview:imageAddress];

    
    UILabel *labelAddress = [[UILabel alloc] initWithFrame:CGRectMake(165, 216, 180, 50)];
    labelAddress.font = [UIFont systemFontOfSize:14];
    labelAddress.numberOfLines = 0;
    labelAddress.text = self.model.address;
    [self.view addSubview:labelAddress];

    

    UILabel *labelActivity = [[UILabel alloc] initWithFrame:CGRectMake(30, 300, 300, 40)];
    labelActivity.font = [UIFont boldSystemFontOfSize:18];
    labelActivity.text = @"活动介绍";
    [self.view addSubview:labelActivity];

    
//    UILabel *labelActivityCintent = [[UILabel alloc] initWithFrame:CGRectMake(30, 350, 300, 300)];
    //    UITextView *textViewCintent = [[UITextView alloc] initWithFrame:CGRectMake(30, 340, 300, 300)];
    UITextView *textViewCintent = [[UITextView alloc] init];
    textViewCintent.text = self.model.content;
    textViewCintent.font = [UIFont systemFontOfSize:14];
    textViewCintent.editable = NO;
    [self.view addSubview:textViewCintent];
    
    
    [textViewCintent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.bottom.mas_equalTo(-10);
        make.top.mas_equalTo(340);
        
        
    }];
    
    
   
    [imageViewPhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(labelTitle.mas_bottom).with.offset(20);
        make.width.mas_equalTo(self.view.frame.size.width / 4);
        //        make.bottom.mas_equalTo(- self.view.frame.size.height / 5 * 3);
        make.right.mas_equalTo(imageViewTime.mas_left).offset(-10);
        //        make.height.mas_equalTo(self.view.frame.size.height / 5 * 3);
        make.height.mas_equalTo(150);
    }];
    

    [imageViewTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imageViewPhoto.mas_right).offset(10);
        make.top.mas_equalTo(labelTitle.mas_bottom).offset(20);
        make.height.width.mas_equalTo(20);
    }];
    
    [labelTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imageViewTime.mas_right).offset(10);
        make.top.mas_equalTo(labelTitle.mas_bottom).offset(20);
        make.right.mas_equalTo(-30);
        
    }];
    

    [imageViewOwner_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imageViewPhoto.mas_right).offset(10);
        make.top.mas_equalTo(imageViewTime.mas_bottom).offset(5);
        make.width.height.mas_equalTo(20);
    }];
    
    
    [labelOwner_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imageViewOwner_name.mas_right).offset(10);
        make.top.mas_equalTo(labelTime.mas_bottom).offset(10);
        make.right.mas_equalTo(-30);
    }];
    

    [imageViewType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imageViewPhoto.mas_right).offset(10);
        make.top.mas_equalTo(imageViewOwner_name.mas_bottom).offset(5);
        make.height.width.mas_equalTo(20);
    }];
    
    [labelType mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.left.mas_equalTo(imageViewType.mas_right).offset(10);
        //        make.top.mas_equalTo(ima)
    }];
    
}

#pragma mark - 导航控制器按钮功能
- (void)handleLeft: (UIBarButtonItem *)button {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)handleRight: (UIBarButtonItem *)button {
    
  
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.model.title;
    self.view.backgroundColor = [UIColor whiteColor];
    

    [self localFile];
    
    [self createView];
    
    self.arr = [NSMutableArray array];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]  initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(handleLeft:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(handleRight:)];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
